package org.jeecg.modules.xdbuse.service.impl;

import org.jeecg.modules.xdbuse.entity.FxjBuse03;
import org.jeecg.modules.xdbuse.entity.FxjBuse0310;
import org.jeecg.modules.xdbuse.entity.FxjBuse0320;
import org.jeecg.modules.xdbuse.mapper.FxjBuse0310Mapper;
import org.jeecg.modules.xdbuse.mapper.FxjBuse0320Mapper;
import org.jeecg.modules.xdbuse.mapper.FxjBuse03Mapper;
import org.jeecg.modules.xdbuse.service.IFxjBuse03Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 放款订单详情
 * @Author: zhaodui
 * @Date:   2020-02-20
 * @Version: V1.0
 */
@Service
public class FxjBuse03ServiceImpl extends ServiceImpl<FxjBuse03Mapper, FxjBuse03> implements IFxjBuse03Service {

	@Autowired
	private FxjBuse03Mapper fxjBuse03Mapper;
	@Autowired
	private FxjBuse0310Mapper fxjBuse0310Mapper;
	@Autowired
	private FxjBuse0320Mapper fxjBuse0320Mapper;
	
	@Override
	@Transactional
	public void saveMain(FxjBuse03 fxjBuse03, List<FxjBuse0310> fxjBuse0310List,List<FxjBuse0320> fxjBuse0320List) {
		fxjBuse03Mapper.insert(fxjBuse03);
		if(fxjBuse0310List!=null && fxjBuse0310List.size()>0) {
			for(FxjBuse0310 entity:fxjBuse0310List) {
				//外键设置
				entity.setQuery01(fxjBuse03.getId());
				fxjBuse0310Mapper.insert(entity);
			}
		}
		if(fxjBuse0320List!=null && fxjBuse0320List.size()>0) {
			for(FxjBuse0320 entity:fxjBuse0320List) {
				//外键设置
				entity.setQuery01(fxjBuse03.getId());
				fxjBuse0320Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FxjBuse03 fxjBuse03,List<FxjBuse0310> fxjBuse0310List,List<FxjBuse0320> fxjBuse0320List) {
		fxjBuse03Mapper.updateById(fxjBuse03);
		
		//1.先删除子表数据
		fxjBuse0310Mapper.deleteByMainId(fxjBuse03.getId());
		fxjBuse0320Mapper.deleteByMainId(fxjBuse03.getId());
		
		//2.子表数据重新插入
		if(fxjBuse0310List!=null && fxjBuse0310List.size()>0) {
			for(FxjBuse0310 entity:fxjBuse0310List) {
				//外键设置
				entity.setQuery01(fxjBuse03.getId());
				fxjBuse0310Mapper.insert(entity);
			}
		}
		if(fxjBuse0320List!=null && fxjBuse0320List.size()>0) {
			for(FxjBuse0320 entity:fxjBuse0320List) {
				//外键设置
				entity.setQuery01(fxjBuse03.getId());
				fxjBuse0320Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		fxjBuse0310Mapper.deleteByMainId(id);
		fxjBuse0320Mapper.deleteByMainId(id);
		fxjBuse03Mapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fxjBuse0310Mapper.deleteByMainId(id.toString());
			fxjBuse0320Mapper.deleteByMainId(id.toString());
			fxjBuse03Mapper.deleteById(id);
		}
	}
	
}
